package com.ysx.server.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.db.sql.Order;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ysx.server.mapper.GoodsMapper;
import com.ysx.server.mapper.OrderDetailMapper;
import com.ysx.server.mapper.OrderMapper;
import com.ysx.server.mapper.UserMapper;
import com.ysx.server.pojo.dto.OrderPageQueryDTO;
import com.ysx.server.pojo.dto.OrderStatusDTO;
import com.ysx.server.pojo.dto.OrderVO;
import com.ysx.server.pojo.entity.Goods;
import com.ysx.server.pojo.entity.OrderDetail;
import com.ysx.server.pojo.entity.Orders;
import com.ysx.server.pojo.entity.User;
import com.ysx.server.pojo.vo.OrdersVO;
import com.ysx.server.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 订单查询 分页 名称 状态
     *
     * @param orderPageQueryDTO
     * @return
     */
    @Override
    public Page<Orders> page(OrderPageQueryDTO orderPageQueryDTO) {
        PageHelper.startPage(orderPageQueryDTO.getCurrent(),orderPageQueryDTO.getPageSize());
        Page<Orders> page=orderMapper.page(orderPageQueryDTO);
        return page;
    }

    @Override
    @Transactional
    public void save(OrderVO orderVO) {
        // 保存订单
        Goods goods = goodsMapper.getById(orderVO.getGoodsId());
        orderVO.setSellId(goods.getUserId());
        orderMapper.save(orderVO);
        // 保存订单详情
        OrderDetail orderDetail = OrderDetail.builder()
                .goodsId(orderVO.getGoodsId())
                .orderId(orderVO.getOrderId())
                .amount(orderVO.getAmount())
                .goodsName(goods.getGoodsName())
                .goodsImage(goods.getGoodsImage())
                .build();
        orderDetailMapper.save(orderDetail);
        //更新商品
//        GoodsDTO goodsDTO = new GoodsDTO();
//        goodsDTO.setId(goods.getId());
//        goodsDTO.setGoodsStatus(2);
//        goodsMapper.update(goodsDTO);
    }

    /**
     * 根据id查询订单信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<OrdersVO> getUserOrder(Long userId) {
        List<Orders> orders= orderMapper.getUserOrder(userId);
        List<OrdersVO> ordersVOS = new ArrayList<>();
        orders.forEach(order->{
            Goods goods = goodsMapper.getById(order.getGoodsId());
            User user = userMapper.getById(order.getUserId());
            OrdersVO ordersVO = new OrdersVO();
            ordersVO.setId(order.getId());
            ordersVO.setOrderId(order.getOrderId());
            ordersVO.setGoodsName(goods.getGoodsName());
            ordersVO.setUserAccount(user.getUserAccount());
            ordersVO.setOrderStatus(order.getOrderStatus());
            ordersVO.setCheckoutTime(order.getCheckoutTime());
            ordersVOS.add(ordersVO);
        });
        return ordersVOS;
    }

    @Override
    public List<OrdersVO> getSellOrder(Long userId) {
        List<Orders> orders= orderMapper.getSellOrder(userId);
        List<OrdersVO> ordersVOS = new ArrayList<>();
        orders.forEach(order->{
            Goods goods = goodsMapper.getById(order.getGoodsId());
            User user = userMapper.getById(order.getUserId());
            OrdersVO ordersVO = new OrdersVO();
            ordersVO.setId(order.getId());
            ordersVO.setOrderId(order.getOrderId());
            ordersVO.setGoodsName(goods.getGoodsName());
            ordersVO.setUserAccount(user.getUserAccount());
            ordersVO.setOrderStatus(order.getOrderStatus());
            ordersVO.setCheckoutTime(order.getCheckoutTime());
            ordersVOS.add(ordersVO);
        });
        return ordersVOS;
    }

    /**
     * 根据id查询订单详情
     *
     * @param id
     * @return
     */
    @Override
    public OrdersVO getDeatil(Long id) {
        Orders order = orderMapper.getOrderDetail(id);
        Goods goods = goodsMapper.getById(order.getGoodsId());
        User user = userMapper.getById(order.getUserId());
        OrdersVO ordersVO = new OrdersVO();
        ordersVO.setId(order.getId());
        ordersVO.setOrderId(order.getOrderId());
        ordersVO.setGoodsName(goods.getGoodsName());
        ordersVO.setUserAccount(user.getUserAccount());
        ordersVO.setOrderStatus(order.getOrderStatus());
        ordersVO.setCheckoutTime(order.getCheckoutTime());
        return ordersVO;
    }

    /**
     * 更新订单状态
     *
     * @param orderStatusDTO
     * @return
     */
    @Override
    public boolean updateStatus(OrderStatusDTO orderStatusDTO) {
        return orderMapper.updateStatus(orderStatusDTO);
    }
}
